﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace CPPEI.Coolzon.UI.StaticPartLib
{
    /// <summary>
    /// Interaction logic for PartGIFImage.xaml
    /// </summary>
    [System.ComponentModel.DisplayName("GIF图片显示组件")]
    [XPartIcon("pack://application:,,,/CPPEI.Coolzon.UI.StaticPartLib;component/Images/PartIcons/GIF图片显示组件.png")]
    public partial class PartGIFImage : ControlPart
    {
        [Category("Other")]
        [DisplayName("图片路径")]
        [Description("ImagePath")]
        [Editor(typeof(XProperty.Editors.XResourceFileEditor), typeof(UI.XProperty.XPropertyValueEditor))]
        [XProperty(XPropertyCategory.Other, "ImagePath", "图片路径")]
        public ResourceFile ImagePath
        {
            get
            {
                return _ImagePath;
            }
            set
            {
                _ImagePath = value;
                Refresh();
            }
        }

        private MediaElement _mediaElement;
        private ResourceFile _ImagePath;

        public PartGIFImage()
        {
            InitializeComponent();
        }

        protected override void OnInit(PartContext context)
        {
            try
            {
                base.OnInit(context);
                _mediaElement = new MediaElement();
                _mediaElement.LoadedBehavior = MediaState.Manual;
                _mediaElement.MediaEnded += (s, e1) =>
                {
                    if (_mediaElement != null)
                    {
                        _mediaElement.Position = _mediaElement.Position.Add(TimeSpan.FromMilliseconds(1));
                    }
                };
                _ctrLayout.Children.Add(_mediaElement);
            }
            catch (Exception ex)
            {
                Common.Logger.ErrorToTag("PartGIFImage", string.Format("GIFImage初始化异常:{0}", ex.Message));
            }
        }
        protected override void OnApplyXProperty()
        {
            base.OnApplyXProperty();
            try
            {
                if (_mediaElement != null)
                {
                    if (ImagePath != null && !string.IsNullOrWhiteSpace(ImagePath.Path))
                    {
                        _mediaElement.Stop();
                        _mediaElement.Source = new Uri(SolutionResourceManager.GetAbsoluteFilePath(ImagePath.Path), UriKind.Absolute);
                        _mediaElement.Play();
                    }
                    else
                    {
                        _mediaElement.Source = null;
                    }
                }
            }
            catch(Exception ex)
            {
                Common.Logger.ErrorToTag("PartGIFImage", string.Format("GIFImage切换图片异常:{0}", ex.Message));
            }
        }
        protected override void OnActive(PartContext context)
        {
            base.OnActive(context);
            if (_mediaElement != null && _mediaElement.Source != null)
            {
                _mediaElement.Play();
            }
        }

        protected override void OnDeactive(PartContext context)
        {
            _mediaElement.Stop();
            _mediaElement.Close();
            base.OnDeactive(context);
        }

        protected override void OnDestory()
        {
            if (_mediaElement != null)
            {
                _mediaElement.Stop();
                _mediaElement.Close();
                _mediaElement = null;
            }
            _ctrLayout.Children.Clear();
            base.OnDestory();
        }
    }
}
